"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.ContextTrace = exports.LoggerLevel = void 0;
// 日志
// src/utils/log4js.ts
const Path = __importStar(require("path"));
const Log4js = __importStar(require("log4js"));
const Util = __importStar(require("util"));
const moment_1 = __importDefault(require("moment")); // 处理时间的工具
const StackTrace = __importStar(require("stacktrace-js"));
const chalk_1 = __importDefault(require("chalk"));
const log4js_1 = __importDefault(require("../config/log4js"));
//日志级别
var LoggerLevel;
(function (LoggerLevel) {
    LoggerLevel["ALL"] = "ALL";
    LoggerLevel["MARK"] = "MARK";
    LoggerLevel["TRACE"] = "TRACE";
    LoggerLevel["DEBUG"] = "DEBUG";
    LoggerLevel["INFO"] = "INFO";
    LoggerLevel["WARN"] = "WARN";
    LoggerLevel["ERROR"] = "ERROR";
    LoggerLevel["FATAL"] = "FATAL";
    LoggerLevel["OFF"] = "OFF";
})(LoggerLevel || (exports.LoggerLevel = LoggerLevel = {}));
// 内容跟踪类
class ContextTrace {
    constructor(context, path, lineNumber, columnNumber) {
        this.context = context;
        this.path = path;
        this.lineNumber = lineNumber;
        this.columnNumber = columnNumber;
    }
}
exports.ContextTrace = ContextTrace;
Log4js.addLayout('Awesome-nest', (logConfig) => {
    return (logEvent) => {
        let moduleName = '';
        let position = '';
        //日志组装
        const messageList = [];
        logEvent.data.forEach((value) => {
            if (value instanceof ContextTrace) {
                moduleName = value.context;
                //显示触发日志的坐标（行/列）
                if (value.lineNumber && value.columnNumber) {
                    position = `${value.lineNumber},${value.columnNumber}`;
                }
                return;
            }
            if (typeof value !== 'string') {
                value = Util.inspect(value, false, 3, true);
            }
            messageList.push(value);
        });
        //日志组成部分
        const messageOutput = messageList.join(' ');
        const positionOutput = position ? `[${position}]` : '';
        const typeOutput = `[${logConfig.type}]${logEvent.pid.toString()} - `;
        const dateOutput = `${(0, moment_1.default)(logEvent.startTime).format('YYYY-MM-DD HH:mm:ss')}`;
        const moduleOutput = moduleName
            ? `[${moduleName}]`
            : '[LoggerService]';
        let levelOutput = `[${logEvent.level}]${messageOutput}`;
        //根据日志级别，用不同颜色区分
        switch (logEvent.level.toString()) {
            case LoggerLevel.DEBUG:
                levelOutput = chalk_1.default.green(levelOutput);
                break;
            case LoggerLevel.INFO:
                levelOutput = chalk_1.default.cyan(levelOutput);
                break;
            case LoggerLevel.WARN:
                levelOutput = chalk_1.default.yellow(levelOutput);
                break;
            case LoggerLevel.ERROR:
                levelOutput = chalk_1.default.red(levelOutput);
                break;
            case LoggerLevel.FATAL:
                levelOutput = chalk_1.default.hex('#DD4C35')(levelOutput);
                break;
            default:
                levelOutput = chalk_1.default.grey(levelOutput);
                break;
        }
        return `${chalk_1.default.green(typeOutput)} ${dateOutput} ${chalk_1.default.yellow(moduleOutput)}`;
    };
});
// 注入配置
Log4js.configure(log4js_1.default);
//实例化
const logger = Log4js.getLogger();
logger.level = LoggerLevel.TRACE;
class Logger {
    static trace(...args) {
        logger.trace(Logger.getStackTrace(), ...args);
    }
    static debug(...args) {
        logger.debug(Logger.getStackTrace(), ...args);
    }
    static log(...args) {
        logger.info(Logger.getStackTrace(), ...args);
    }
    static info(...args) {
        logger.info(Logger.getStackTrace(), ...args);
    }
    static warn(...args) {
        logger.warn(Logger.getStackTrace(), ...args);
    }
    static warning(...args) {
        logger.warn(Logger.getStackTrace(), ...args);
    }
    static error(...args) {
        logger.error(Logger.getStackTrace(), ...args);
    }
    static fatal(...args) {
        logger.fatal(Logger.getStackTrace(), ...args);
    }
    static access(...args) {
        const loggerCustom = Log4js.getLogger('http');
        loggerCustom.info(Logger.getStackTrace(), ...args);
    }
    // 日志追踪，可以追溯到哪个文件、第几行第几列
    static getStackTrace(deep = 2) {
        const stackList = StackTrace.getSync();
        const stackInfo = stackList[deep];
        const lineNumber = stackInfo.lineNumber;
        const columnNumber = stackInfo.columnNumber;
        const fileName = stackInfo.fileName;
        const basename = Path.basename(fileName);
        return `${basename}(line: ${lineNumber}, column: ${columnNumber}): \n`;
    }
}
exports.Logger = Logger;
